<template>
  <div class="content">
    <div class="sign-up-container">
      <el-form ref="userForm" :model="params">
        <el-form-item
          class="input-prepend restyle"
          prop="nickname"
          :rules="[
            {
              required: true,
              message: '昵称不能为空',
              trigger: 'blur',
            },
          ]"
        >
          <div>
            <el-input
              type="text"
              placeholder="请输入您的昵称"
              v-model="params.nickname"
              prefix-icon="el-icon-user"
            />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend restyle no-radius"
          prop="mobile"
          :rules="[
            {
              required: true,
              message: '手机号码不能为空',
              trigger: 'blur',
            },
            { validator: checkPhone, trigger: 'blur' },
          ]"
        >
          <div>
            <el-input
              type="text"
              placeholder="请输入手机号码"
              v-model="params.mobile"
              prefix-icon="el-icon-mobile"
            />
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend restyle no-radius"
          prop="code"
          :rules="[
            {
              required: true,
              message: '请输入验证码',
              trigger: 'blur',
            },
          ]"
        >
          <div
            style="width: 100%; display: block; float: left; position: relative"
          >
            <el-input
              type="text"
              placeholder="请输入验证码"
              v-model="params.code"
              prefix-icon="el-icon-message"
            />
          </div>
          <div class="codetest">
            <el-button type="text" @click="getCodeFun()" :disabled="!sending">{{
              codeTest
            }}</el-button>
          </div>
        </el-form-item>

        <el-form-item
          class="input-prepend"
          prop="password"
          :rules="[
            {
              required: true,
              message: '密码不能为空',
              trigger: 'blur',
            },
          ]"
        >
          <div>
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="params.password"
              prefix-icon="el-icon-lock"
            />
          </div>
        </el-form-item>

        <div class="btn">
          <input
            type="button"
            class="sign-up-button"
            value="注册"
            @click="validateRegister()"
          />
        </div>
        <p class="sign-up-msg">
          点击 “注册” 即表示您同意并愿意遵守简书
          <br />
          <a target="_blank" href="http://www.jianshu.com/p/c44d171298ce"
            >用户协议</a
          >
          和
          <a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a>
          。
        </p>
      </el-form>
      <!-- 更多注册方式 -->
      <div class="more-sign">
        <h6>社交帐号直接注册</h6>
        <ul>
          <li>
            <a
              id="weixin"
              class="weixin"
              target="_blank"
              :href="this.wxUrl + '/api/ucenter/wx/login'"
              ><!--TODO 微信登录接口-->
              <i class="iconfont icon-weixin" />
            </a>
          </li>
          <!-- qq暂时用不上 -->
          <!-- <li>
            <a id="qq" class="qq" target="_blank" href="#"
              ><i class="iconfont icon-qq"
            /></a>
          </li> -->
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import registerApi from "@/api/register";

export default {
  layout: "sign",
  data() {
    return {
      params: {
        //封装注册输入数据
        mobile: "",
        code: "", //验证码
        nickname: "",
        password: "",
      },
      sending: true, //是否发送验证码
      second: 60, //倒计时间
      codeTest: "获取验证码",
      wxUrl: process.env.baseUrl,
    };
  },
  methods: {
    // 点击注册进行字段验证
    validateRegister() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          this.submitRegister();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //注册提交的方法
    submitRegister() {
      registerApi.registerMember(this.params).then((response) => {
        if (response.data.success) {
          //提示注册成功
          this.$message.success('注册成功');
          //跳转登录页面
          this.$router.push({ path: "/login" });
        }
      });
    },
    timeDown() {
      let result = setInterval(() => {
        --this.second;
        this.codeTest = this.second + "秒后可重新发送";
        if (this.second < 1) {
          clearInterval(result);
          this.sending = true;
          this.disabled = false;
          this.second = 60;
          this.codeTest = "获取验证码";
        }
      }, 1000);
    },
    //通过输入手机号发送验证码
    getCodeFun() {
      registerApi.sendCode(this.params.mobile).then((response) => {
        this.sending = false;
        //调用倒计时的方法
        this.timeDown();
      });
    },

    checkPhone(rule, value, callback) {
      //debugger
      if (!/^1[345789]\d{9}$/.test(value)) {
        return callback(new Error("手机号码格式不正确"));
      }
      return callback();
    },
  },
};
</script>
